package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Activity;
import com.qk.entity.dto.activity.ActivityDTO;
import com.qk.management.mapper.ActivityMapper;
import com.qk.management.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author fantuan
 * @created 2025/10/9 10:55
 * @description
 */
@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    @Override
    public PageResult<Activity> page(ActivityDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<Activity> result = activityMapper.selectByPage(dto);
        return PageResult.<Activity>builder()
                .total((int) ((Page) result).getTotal())
                .rows(result)
                .build();
    }

    @Override
    public void deleteById(Integer id) {
        activityMapper.deleteById(id);
    }

    @Override
    public void add(Activity activity) {
        boolean hasNull = BeanUtil.hasNullField(activity,"id","discount","voucher","createTime","updateTime");
        if (hasNull) {
            throw new RuntimeException("参数错误");
        }
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.insert(activity);
    }

    @Override
    public Activity getActivityById(Integer id) {

        return activityMapper.selectById(id);
    }

    @Override
    public void update(Activity activity) {
        boolean hasNull = BeanUtil.hasNullField(activity,"id","discount","voucher","createTime","updateTime");
        if (hasNull) {
            throw new RuntimeException("参数错误");
        }
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.update(activity);
    }

    @Override
    public List<Activity> listByType(Integer type) {
        return activityMapper.listByType(type);
    }
}
